﻿using System;
using System.Linq;
using Moe.ECamel.Common.Entities;

namespace Moe.ECamel.Server.BL
{
	public class SalaryBL : ECamelBL
	{
		public SalaryBL(ECamelDataContext dataContext) 
			: base(dataContext)
		{
		}

		public bool CreateSalaryIfNeeded(Employee employee, DateTime payDate)
		{
			var existingsalary = (from s in dataContext.Salaries
			           where s.PayedOn == payDate && s.EmployeeId == employee.EmployeeId
			           select s).SingleOrDefault();
			if (existingsalary != null)
				return false;

			var salary = new Salary {EmployeeId = employee.EmployeeId, Amount = employee.Salary, PayedOn = payDate};
			dataContext.Salaries.InsertOnSubmit(salary);

			return true;
		}
	}
}
